﻿/*
 * Programer: JR Padfield
 * Description: This class handles all the games graphics.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace TheWorld.Global
{
    class GfxManager
    {
        #region Textures Arrays

        // XNA Textures & Fonts
        public List<SpriteFont> FONT_TEXTURE = new List<SpriteFont>();
        public List<Texture2D> Tilesets = new List<Texture2D>();
        public List<Texture2D> Sprites = new List<Texture2D>();
        public List<Texture2D> Faces = new List<Texture2D>();
        public List<Texture2D> Gui = new List<Texture2D>();
        public List<Texture2D> Buttons = new List<Texture2D>();
        public List<Texture2D> Buttons_C = new List<Texture2D>();
        public List<Texture2D> Buttons_H = new List<Texture2D>();
        public List<Texture2D> Backgrounds = new List<Texture2D>();

        #endregion

        #region Texture Counts

        // Count
        public int Tilesets_Count = 1;
        public int Sprites_Count = 1;
        public int Faces_Count = 1;
        public int Gui_Count = 1;
        public int Buttons_Count = 1;
        public int Buttons_C_Count = 1;
        public int Buttons_H_Count = 1;
        public int Backgrounds_Count = 1;
        public int FONT_TEXTURE_Count = 1;

        #endregion

        #region Texture Drawing Rectangle

        // XNA Rectangle
        private Rectangle _dRect = new Rectangle(0, 0, 0, 0);
        private Rectangle _sRect = new Rectangle(0, 0, 0, 0);

        #endregion

        #region Constructor and Singleton

        // Private Constructor
        private GfxManager()
        {
            FONT_TEXTURE.Add(null);
            Tilesets.Add(null);
            Sprites.Add(null);
            Faces.Add(null);
            Gui.Add(null);
            Buttons.Add(null);
            Buttons_C.Add(null);
            Buttons_H.Add(null);
            Backgrounds.Add(null);
        }

        // Singleton Accessor
        static GfxManager instance;
        public static GfxManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new GfxManager();
                }

                return instance;
            }
        }

        #endregion

        #region Load Textures

        // Texture Engine
        public void initTextures()
        {
            FileStream stream;
            ContentManager gameContent = GameContentManager.Instance.gameContent;

            #region Tiles

            // ######################
            // ## Tileset Textures ##
            // ######################
            // Loop through folder scanning for textures to load
            string currentFolder = "Tiles\\";

            DirectoryInfo dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Tilesets_Count + ".png") == true)
            {
                stream = new FileStream("Content/Tiles/" + Tilesets_Count + ".png", FileMode.Open, FileAccess.Read);
                Tilesets.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Tilesets_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Tilesets_Count;

            #endregion

            #region Sprites

            // #####################
            // ## Sprite Textures ##
            // #####################
            // Loop through folder scanning for textures to load
            currentFolder = "Sprites\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Sprites_Count + ".png") == true)
            {
                stream = new FileStream("Content/Sprites/" + Sprites_Count + ".png", FileMode.Open, FileAccess.Read);
                Sprites.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Sprites_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Sprites_Count;

            #endregion

            #region Faces

            // ####################
            // ## Faces Textures ##
            // ####################
            // Loop through folder scanning for textures to load
            currentFolder = "Faces\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Faces_Count + ".png") == true)
            {
                stream = new FileStream("Content/Faces/" + Faces_Count + ".png", FileMode.Open, FileAccess.Read);
                Faces.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Faces_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Faces_Count;

            #endregion

            #region GUI

            // ##################
            // ## Gui Textures ##
            // ##################

            // Loop through folder scanning for textures to load
            currentFolder = "GUI\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Gui_Count + ".png") == true)
            {
                stream = new FileStream("Content/GUI/" + Gui_Count + ".png", FileMode.Open, FileAccess.Read);
                Gui.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Gui_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Gui_Count;

            #endregion

            #region Backgrounds

            // ##################
            // ##  Backgrounds ##
            // ##################

            // Loop through folder scanning for textures to load
            currentFolder = "Backgrounds\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Backgrounds_Count + ".png") == true)
            {
                stream = new FileStream("Content/Backgrounds/" + Backgrounds_Count + ".png", FileMode.Open, FileAccess.Read);
                Backgrounds.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Backgrounds_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Backgrounds_Count;

            #endregion

            #region Buttons

            // #####################
            // ## Button Textures ##
            // #####################

            // Loop through folder scanning for textures to load
            currentFolder = "Buttons\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Buttons_Count + ".png") == true)
            {
                stream = new FileStream("Content/Buttons/" + Buttons_Count + ".png", FileMode.Open, FileAccess.Read);
                Buttons.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Buttons_Count++;
            }

            // decrement int due to 1 too many calls to the increment
            --Buttons_Count;

            #endregion

            #region Buttcon Click

            // ###########################
            // ## Button Click Textures ##
            // ###########################

            // Loop through folder scanning for textures to load
            currentFolder = "Buttons\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Buttons_C_Count + "_C.png") == true)
            {
                stream = new FileStream("Content/Buttons/" + Buttons_C_Count + "_C.png", FileMode.Open, FileAccess.Read);
                Buttons_C.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Buttons_C_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Buttons_C_Count;

            #endregion

            #region Button Hover

            // ###########################
            // ## Button Hover Textures ##
            // ###########################

            // Loop through folder scanning for textures to load
            currentFolder = "Buttons\\";
            dir = new DirectoryInfo("Content\\" + currentFolder);
            while (File.Exists(dir.FullName + Buttons_H_Count + "_H.png") == true)
            {
                stream = new FileStream("Content/Buttons/" + Buttons_H_Count + "_H.png", FileMode.Open, FileAccess.Read);
                Buttons_H.Add(Texture2D.FromStream(Client.graphics.GraphicsDevice, stream));
                Buttons_H_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --Buttons_H_Count;

            #endregion

            #region Fonts

            // ###########
            // ## FONTS ##
            // ###########
            // Load Fonts //

            // Loop through folder scanning for textures to load
            dir = new DirectoryInfo("Content\\Fonts\\");
            while (File.Exists(dir.FullName + FONT_TEXTURE_Count + ".xnb") == true)
            {
                FONT_TEXTURE.Add(gameContent.Load<SpriteFont>("Fonts\\" + FONT_TEXTURE_Count));
                FONT_TEXTURE_Count++;
            }
            // decrement int due to 1 too many calls to the increment
            --FONT_TEXTURE_Count;

            #endregion
        }

        #endregion

        // String Width //
        public int getStringWidth(int Font, string text)
        {
            Vector2 value = FONT_TEXTURE[Font].MeasureString(text);
            return (int)value.X;
        }

        // String Height //
        public int getStringHeight(int font, string text)
        {
            Vector2 value = FONT_TEXTURE[font].MeasureString(text);
            return (int)value.Y;
        }
    }
}
